# -*- coding: utf-8 -*-
# בש״ד
#
# Miramar CMS for Django - Part of the Cuba Libre Project
# Copyright (C) 2009, Joshua "jag" Ginsberg <jag@flowtheory.net>
# 
# Por mi amor, que inspira a mi cada respiración y que echa de Cuba
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.db import models
from django.core import exceptions
from mirimar.api import IMirimarMarkupNamespace

def parse_manager_string(manager_string):
    app_name, model, manager_attr = manager_string.split('.')
    model_obj = models.get_model(app_name, model)
    if not model_obj:
        raise exceptions.ImproperlyConfigured, \
            u'Could not load %s.%s' % (app_name, model)
    try:
        return getattr(model_obj, manager_attr)
    except AttributeError:
        raise exceptions.ImproperlyConfigured, \
            u'Model %s.%s has no attribute %s' % (app_name, model, manager_attr)

def get_handler_for_namespace(namespace):
    for id, cmp_namespace in IMirimarMarkupNamespace.get_namespace_identifier():
        if namespace == cmp_namespace:
            component = IMirimarMarkupNamespace.get_component_by_id(id)
            return (component.get_manager_obj(),
                    component.get_detail_url_name(),
                    component.get_create_url_name())
    raise KeyError
